/**
 * Created by billy on 2017/4/25.
 */
var g = require("nodeLib");
var _sql;
var _sqlGame;
var _file = g.data.file.get("user");
var arr = [];
var tool = require("../utils/tool");
var voiceTool = require("./func/voiceTool")
module.exports = class {
	constructor()
	{
		_file.add(__projpath("./module/sql/user"));
		_sql = g.data.manager.getManager("myGameClass");
		_sqlGame = g.data.manager.getManager("myGame");
		this.add("getList", this.getList);
		this.add("sendGame", this.sendGame);
		this.add("getGame", this.getGame);
		this.add("createGame", this.createGame);
		this.add("getClassList", this.getClassList);
		this.add("getForm", this.getForm);
		this.add("getClass", this.getClass);
	}

	getList($data, $success, $error, $client)
	{
// 		trace(1)
		var sqlStr = _file.get("selectClass.sql");
		_sql.query(sqlStr, ($list) => {
			$success($list);
		})
	}

	sendGame($data, $success, $error, $client)
	{
		if (!hasData($data, "content", "title", "class", "desc", "beforeList", "afterList", "target", "age", "tips"))
		{
			$error({code: 9999});
			return;
		}
		$data = filterSql($data);
		var _sqlStr = _file.get("selectGame.sql");
		_sql.query(_sqlStr, ($list) => {
			var sqlStr = _file.get("addGame.sql",
				{
					class: $data.class,
					title: $data.title,
					afterSpeak: $data.beforeList,
					beforeSpeak: $data.afterList,
					content: $data.content,
					desc: $data.desc,
					target: $data.target,
					tips: $data.tips,
					age: $data.age
				});
			_sql.query(sqlStr, ($list) => {
				$success('')
			})
		})
	}

	getGame($data, $success, $error, $client)
	{
		var sqlStr = _file.get("selectGame.sql");

		_sql.query(sqlStr, ($list) => {
			$success($list);
// 			$error('CVBXCVdsdf');
		})
	}

	createGame($data, $success, $error, $client)
	{
		if (!hasData($data, "id"))
		{
			$error({code: 9999});
			return;
		}
		$data = filterSql($data);
		var _sqlString = _file.get("selectModel.sql", {id: $data.id});
		_sql.query(_sqlString, ($list) => {
//
			var arrObj = $list[0];
			var sqlString = _file.get("selectGameList.sql");
			_sqlGame.query(sqlString, ($list) => {
				var id = $list.length + 1;
				var time = new Date();
//				自动添加照片和音频字段
				var beforeList = arrObj.beforeSpeak.split('%');
				var afterList = arrObj.afterSpeak.split('%');
				g.fs.mkdirSync(__projpath(`/game/${id}`));
				voiceTool.getList(beforeList, arrObj);
				var img1 = '';
				var img2 = '';
				var music1 = '';
				var music2 = '';
//				当图片不再重复的时候进行更改后面的“1”改成“2”
				var img = tool.getString(img1, beforeList, 1, ".png") + '&' + tool.getString(img2, afterList, 1, ".png");
				var music = tool.getString(music1, beforeList, 1, ".mp3") + '&' + tool.getString(music2, afterList, 1, ".mp3");
//				生成游戏json字段
				var obj = {};
				obj.gameTitle = arrObj.content;
				obj.beforeList = arrObj.beforeSpeak;
				obj.afterList = arrObj.afterSpeak;
				obj.music = music;
				obj.img = img;
				g.fs.writeFileSync(__projpath(`/game/${id}/game.json`), JSON.stringify(obj));
				var sqlStr = _file.get("createGame.sql",
					{
						class: arrObj.class,
						title: arrObj.title,
						content: arrObj.content,
						img: img,
						music: music,
						desc: arrObj.desc,
						beforeSpeak: arrObj.beforeSpeak,
						afterSpeak: arrObj.afterSpeak,
						target: arrObj.target,
						age: arrObj.age,
						tips: arrObj.tips,
						createTime: parseInt(time.getTime() / 1000)
					});
				_sqlGame.query(sqlStr, ($list) => {
					var _sqlStr = _file.get("deleteGame.sql", {id: $data.id});
					_sql.query(_sqlStr, ($list) => {
						$success($list);
					})
				})
			})
		})
	}

	getClassList($data, $success, $error, $client)
	{
		if (!hasData($data, "class"))
		{
			$error({code: 9999});
			return;
		}
		$data = filterSql($data);
		var sqlStr = _file.get("selectClassGame.sql", {class: $data.class});
		_sql.query(sqlStr, ($list) => {
			var list = $list[0].concat($list[1])
			$success(list);
// 			$error('CVBXCVdsdf');
		})
	}

	getForm($data, $success, $error, $client)
	{
		$data = filterSql($data);

		if (!hasData($data, "formId"))
		{
			$error({code: 9999});
			return;
		}
		var sqlStr = _file.get("selectBase_from_getList.sql",
			{
				formId: $data.formId
			});
		_sqlGame.query(sqlStr, ($list) => {
			g.fs.mkdirSync(__projpath("8"))
			g.fs.writeFileSync(__projpath("8/form.json"), JSON.stringify($list));
			$success($list);
		})
	}

	getClass($data, $success, $error, $client)
	{
		$data = filterSql($data);

		var sqlStr = _file.get("selectForm_Class.sql");
//		trace(sqlStr);
		_sqlGame.query(sqlStr, ($list) => {
			g.fs.mkdirSync(__projpath("9"))
			g.fs.writeFileSync(__projpath("9/class.json"), JSON.stringify($list));
			$success($list);
		})
	}
}